home *** CD-ROM | disk | FTP | other *** search
/ Windows 6-Pak - Disc 5 / Windows 6-Pak (InfoMagic) (Disc 5) (1999).ISO / C&C++Tools / sbparser.exe / cppb / PlotTest.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  1998-07-01  |  4.5 KB  |  157 lines

  1. //---------------------------------------------------------------------------
  2. #include <vcl.h>
  3. #pragma hdrstop
  4.  
  5. #include "PlotTest.h"
  6. #include "ParseDemo.h"
  7. //---------------------------------------------------------------------------
  8. #pragma package(smart_init)
  9. #pragma resource "*.dfm"
  10. TFPlotTest *FPlotTest;
  11. //---------------------------------------------------------------------------
  12. __fastcall TFPlotTest::TFPlotTest(TComponent* Owner)
  13.     : TForm(Owner)
  14. {
  15. }
  16. //---------------------------------------------------------------------------
  17.  
  18. void __fastcall TFPlotTest::BExitClick(TObject *Sender)
  19. {
  20.     Close();
  21. }
  22. //---------------------------------------------------------------------------
  23.  
  24. void __fastcall TFPlotTest::PBPlotPaint(TObject *Sender)
  25. {
  26.     TPaintBox *thePBPlot = (TPaintBox *)Sender;
  27.     thePBPlot->Canvas->Pen->Color = clGray;
  28.  
  29.     //Draw coord
  30.     thePBPlot->Canvas->MoveTo(0, thePBPlot->Height/2);
  31.     thePBPlot->Canvas->LineTo(thePBPlot->Width, thePBPlot->Height/2);
  32.     thePBPlot->Canvas->MoveTo(thePBPlot->Width/2, 0);
  33.     thePBPlot->Canvas->LineTo(thePBPlot->Width/2, thePBPlot->Height);
  34.  
  35.     //Draw function
  36.     PlotFunction(thePBPlot);
  37. }
  38. //---------------------------------------------------------------------------
  39.  
  40. void TFPlotTest::PlotFunction(TPaintBox *thePBPlot)
  41. {
  42.     thePBPlot->Canvas->Pen->Color = clBlack;
  43.  
  44.     int BaseIn;
  45.     int BaseOut;
  46.     int AngularUnit;
  47.     char tempstr[50];
  48.  
  49.     //Get data (Base for Input/output and angular unit) from the main form
  50.     FParser->GetBaseAndAngUnit(this, &BaseIn, &BaseOut, &AngularUnit);
  51.  
  52.     long double varValues[6];
  53.     bool err = false;
  54.  
  55.     //Get data (values of the variables) from the main form
  56.     err = FParser->GetVarValues(this, BaseIn, varValues);
  57.     if (err)
  58.         return;
  59.  
  60.     long double Result;
  61.     AnsiString funcStr = MFunction->Text.c_str();
  62.     long ctime = GetCurrentTime();
  63.  
  64.     Screen->Cursor = crHourGlass;
  65.        //Create an sbParser object and receive its HANDLE
  66.     HANDLE hParser = CreateNewParser(funcStr.c_str(), BaseIn, 6, "xyzuvw");
  67.     Screen->Cursor = crDefault;
  68.        if (GetIsError(hParser)) {
  69.         char errText[50];
  70.  
  71.         //An error has occurred (see documentation)
  72.         MessageBox(Handle, ldtochar(errText, GetGlobalError(hParser)),
  73.             "Errornumber in Function (see documentation)",
  74.             MB_ICONERROR);
  75.  
  76.            //Delete of the sbParser-object
  77.         DeleteParser(hParser);
  78.         return;
  79.     }
  80.        SetAngularUnitTo(hParser, AngularUnit);
  81.  
  82.     //Creation time
  83.     ctime = GetCurrentTime() - ctime;
  84.     LtheTime0->Caption = ldtochar(tempstr, (long double)ctime/1000, 12);
  85.  
  86.     long double diffx = 20/(long double)thePBPlot->Width;
  87.     long double facty = 20/(long double)thePBPlot->Height;
  88.     bool firstPlot = true;
  89.     long plotxPos, plotyPos;
  90.  
  91.     //Reset time
  92.     ctime = GetCurrentTime();
  93.     Screen->Cursor = crHourGlass;
  94.     if (err) {
  95.         ctime = 0;
  96.     }
  97.     else
  98.     {
  99.       //Calculate and draw function
  100.       for (long double xPos = -10; xPos <= 10; xPos = xPos+diffx) {
  101.         varValues[0] = xPos;
  102.  
  103.         //Compute the result
  104.         Result = GetResultExt(hParser, varValues);
  105.         err = GetIsError(hParser);
  106.  
  107.         plotxPos = xPos*1/diffx+thePBPlot->Width/2+1;
  108.         plotyPos = -1*Result*1/facty+thePBPlot->Height/2;
  109.  
  110.         //Plot out of drawing rect?
  111.         if (plotyPos < 0) {
  112.             plotyPos = -1;
  113.         }
  114.         if (plotyPos > thePBPlot->Height) {
  115.             plotyPos = thePBPlot->Height;
  116.         }
  117.  
  118.         //error: Jump over - else: plot
  119.         if ((firstPlot == true) || (err == true)) {
  120.             thePBPlot->Canvas->MoveTo(plotxPos, plotyPos);
  121.             if (err == false) {
  122.                 firstPlot = false;
  123.             }
  124.             else {
  125.                 firstPlot = true;
  126.             }
  127.         }
  128.         else
  129.             thePBPlot->Canvas->LineTo(plotxPos, plotyPos);
  130.       }
  131.     }
  132.     Screen->Cursor = crDefault;
  133.  
  134.     //Calculation time
  135.     ctime = GetCurrentTime() - ctime;
  136.     LtheTime->Caption = ldtochar(tempstr, (long double)ctime/1000, 12);
  137.  
  138.        //Delete of the sbParser-object
  139.     DeleteParser(hParser);
  140. }
  141.  
  142. void __fastcall TFPlotTest::FormShow(TObject *Sender)
  143. {
  144.     this->MFunction->Lines = FParser->MFunction->Lines;
  145.     LtheTime->Caption = "0";
  146. }
  147. //---------------------------------------------------------------------------
  148.  
  149. void __fastcall TFPlotTest::BRedrawClick(TObject *Sender)
  150. {
  151.     PBPlot->Invalidate();
  152. }
  153. //---------------------------------------------------------------------------
  154.  
  155.  
  156.  
  157.